import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import vue2 from '@vitejs/plugin-vue2'
import vue2Jsx from '@vitejs/plugin-vue2-jsx'
import checker from 'vite-plugin-checker'
const absPathByDirname = retivePath => path.resolve(__dirname, retivePath)
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, absPathByDirname('./env'))
  console.log(env)
  return {
    envDir: './env',
    base: env.VITE_BASE_URL,
    resolve: {
      alias: {
        '@': absPathByDirname('./src')
      },
      extensions: ['.vue', '.js', '.json']
    },
    server: {
      open: false
    },
    plugins: [
      vue2(),
      vue2Jsx(),
      checker({
        eslint: {
          lintCommand: 'eslint "./src/**/*"'
        }
      })
    ],
    build: {
      sourcemap: false,
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString()
            }
          },
          entryFileNames: 'js/[name].[hash].js',
          assetFileNames: '[ext]/[name].[hash].[ext]',
          chunkFileNames: chunkInfo => {
            const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : []
            const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'
            return `js/${fileName}/[name].[hash].js`
          }
        }
      }
    },
    css: {
      devSourcemap: true,
      preprocessorOptions: {
        stylus: {
          imports: [
            path.resolve(__dirname, 'src/assets/stylus/varibles.styl'),
            path.resolve(__dirname, 'src/assets/stylus/functions.styl')
          ]
        }
      }
    }
  }
})
